/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tesoreria.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cip.esymons.general.be.PersonaJuridica;
import org.cip.esymons.tesoreria.be.CobroPlanilla;
import org.cip.esymons.tesoreria.be.DetCobroPlanilla;
import org.cip.esymons.tesoreria.be.EnumEstadoCobroPlanilla;
import org.cip.esymons.tesoreria.be.EnumMeses;

/**
 *
 * @author Ingrid
 */
@Stateless
public class CobroPlanillaFacade implements CobroPlanillaFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(CobroPlanilla cobroPlanilla) {
        em.persist(cobroPlanilla);
    }

    public void edit(CobroPlanilla cobroPlanilla) {
        em.merge(cobroPlanilla);
    }

    public void remove(CobroPlanilla cobroPlanilla) {
        em.remove(em.merge(cobroPlanilla));
    }

    public CobroPlanilla find(Object id) {
        try {
            return em.find(CobroPlanilla.class, id);
        } catch (Exception e) {
            return null;
        }
        
    }

    public List<CobroPlanilla> findAll() {
        try {
            return em.createQuery("select object(o) from CobroPlanilla as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }

    }

    public List<PersonaJuridica> listaTodasEmpresasConPlanillaColegiados(){
        try {
            return em.createQuery("select cp.empresaOInstitucion from CobroPlanilla as cp group by cp.empresaOInstitucion").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<CobroPlanilla> colegiadosActivosYCesantes(PersonaJuridica empresa){
        try {
            List<CobroPlanilla> lista = em.createQuery("select object(cp) from CobroPlanilla as cp where cp.empresaOInstitucion=?1 order by cp.regimen").setParameter(1,empresa).getResultList();
            for(CobroPlanilla cp : lista){
                List<DetCobroPlanilla> listaDCP = new ArrayList<DetCobroPlanilla>();
                for(DetCobroPlanilla dcp :listaDetalleCobroPlanilla(cp)){
                    if(dcp.getDcpEstado().equals(EnumEstadoCobroPlanilla.Vigente)){
                        listaDCP.add(dcp);
                    }
                }
                cp.setDetCobroPlanillaList(listaDCP);
            }
            return lista;

        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<DetCobroPlanilla> listaDetalleCobroPlanilla(CobroPlanilla cobroPlanilla){
        try {
            return em.createQuery("select dcp from DetCobroPlanilla as dcp Where dcp.cplCodigo=?1 order by dcp.dcpAnio DESC").setParameter(1, cobroPlanilla).getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<DetCobroPlanilla> listaPagosXCobroPlanilla(EnumMeses mes,int anio){
        try {
            return em.createQuery("Select dcp from DetCobroPlanilla as dcp Where dcp.mes=?1 and dcp.dcpAnio=?2 and dcp.dcpEstado=?3").setParameter(1, mes).setParameter(2, anio).setParameter(3, EnumEstadoCobroPlanilla.Vigente).getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<DetCobroPlanilla> listaCobroPlanillaXColegiado(Long colegiado,EnumMeses mes,int anio,EnumEstadoCobroPlanilla enumEstadoCobroPlanilla){
        try {
            return em.createQuery("Select dcp from DetCobroPlanilla as dcp Where dcp.mes=?1 and dcp.dcpAnio=?2 and dcp.dcpEstado=?3 and dcp.cplCodigo.colegiado.solCodigo=?4").setParameter(1, mes).setParameter(2, anio).setParameter(3, enumEstadoCobroPlanilla).setParameter(4, colegiado).getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    /**
     * @return the em
     */
    public EntityManager getEm() {
        return em;
    }

}
